<?php


namespace App\Imports;

use App\Models\ArchiveManageVolume;
use App\Models\LedgerEquipment;
use App\Models\Storeroom;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithColumnLimit;
use Maatwebsite\Excel\Concerns\WithLimit;
use Maatwebsite\Excel\Concerns\WithStartRow;

/**
 * Class BookTitleImport
 * @package App\Imports
 * 适用于 29号文件
 */
class LedgerEquipmentImport implements ToModel, WithChunkReading, WithStartRow, WithColumnLimit, WithBatchInserts, WithLimit, SkipsEmptyRows
{
    protected $chunkSize;
    protected $startRow;
    protected $endColumn;
    protected $batchSize;
    protected $tmpKey;
    protected $limit;
    protected $docArchiveInfoKey;
    protected $importCountKey;
    protected $projectId;
    protected $content;
    protected $fileNumber;
    protected $importDataIds;

    public function __construct($importCountKey, $projectId)
    {
        $this->limit             = 10000; //只读取前10000行
        $this->chunkSize         = 100;  //将数据拆成小块读取
        $this->startRow          = 1;    //从第几行开始
        $this->endColumn         = 'F';  //指定到那一列为止
        $this->batchSize         = 100;  //批量写入的数据大小
        $this->docArchiveInfoKey = 'import_ledger_equipment';
        $this->importCountKey    = $importCountKey;
        $this->tmpKey            = 'data';
        $this->projectId         = $projectId;
        $this->importDataIds     = [];
    }

    /**
     * 导入数据
     */
    public function model(array $row)
    {
        if ($row[0] && is_numeric($row[0]) && format_table_label($row[1])) {
            $res = LedgerEquipment::query()->updateOrCreate(
                [ //查询条件
                    'project_id' => $this->projectId,
                    'name'       => format_table_label($row[1]),
                    'model_num'  => format_table_label($row[2]),
                ],
                [ //数据
                    'serial_no' => format_table_label($row[0]),
                    'remark'    => format_table_label($row[3]),
                ]);
            $this->importDataIds[] = $res->id;
            redis()->incr($this->importCountKey); //导入的数据量加
        } elseif (str_contains($row[0], '编制人：')) {
            $endRow   = format_table_label($row[0]);
            $tmp1     = str_replace('编制人：', '', $endRow);
            $pos1     = strpos($tmp1, '审核人：');
            $prepare  = substr($tmp1, 0, $pos1);
            $tmp2     = str_replace($prepare, '', $tmp1);
            $tmp3     = str_replace('审核人：', '', $tmp2);
            $pos2     = strpos($tmp3, '日期：');
            $reviewer = substr($tmp3, 0, $pos2);
            $tmp4     = str_replace($reviewer, '', $tmp3);
            $date     = str_replace('日期：', '', $tmp4);
            LedgerEquipment::query()->whereIn('id', $this->importDataIds)->update([
                'prepare_person' => $prepare,
                'reviewer'       => $reviewer,
                'record_date'    => $date,
            ]);
        }
    }

    /**
     * 将数据拆成小块进行导入，减小内存占用
     *
     * @return int
     */
    public function chunkSize(): int
    {
        return $this->chunkSize;
    }

    /**
     * 直接过滤表头，第一块数据含表头
     *
     * @return int
     */
    public function startRow(): int
    {
        return $this->startRow;
    }

    /**
     * 指定到那一列为止，G代表读取到G列就不再读取
     *
     * @return string
     */
    public function endColumn(): string
    {
        return $this->endColumn;
    }

    /**
     * 批量写入的数据大小
     *
     * @return int
     */
    public function batchSize(): int
    {
        return $this->batchSize;
    }

    public function limit(): int
    {
        return $this->limit;
    }

}
